package com.zhima.controller.plain;

import com.zhima.dto.ZhimaOjResult;
import com.zhima.service.impl.EmailCheckCodeServiceImpl;
import com.zhima.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/email")
public class EmailController {
    @Autowired
    private EmailCheckCodeServiceImpl emailCheckCodeService;

    /**
     * 向指定邮箱发送验证码
     *
     * @param toEmail
     * @return
     */
    @PostMapping("/{toEmail}")
    public ZhimaOjResult sendEmailCheckCode(@PathVariable("toEmail") String toEmail) {
        emailCheckCodeService.sendCheckCode(toEmail);
        return ZhimaOjResult.success();
    }

    /**
     * 向登录用户邮箱发送验证码
     *
     * @return
     */
    @PostMapping
    public ZhimaOjResult sendEmailCheckCode() {
        emailCheckCodeService.sendCheckCode(UserHolder.get().getEmail());
        return ZhimaOjResult.success();
    }
}
